package com.bjdd.jdy.ui.home;




import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.bjdd.jdy.R;
import com.bjdd.jdy.baidu.MyService;
import com.bjdd.jdy.base.BaseFragment;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.entity.User;
import com.bjdd.jdy.ui.hotMap.HotMapFragment;
import com.bjdd.jdy.ui.working.WorkingFragment;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment<HomePresenter, HomeModel> implements HomeContract.View {


    @Bind(R.id.tv_location)
    TextView tvLocation;
    @Bind(R.id.img_home_hotmap)
    ImageView imgMap;
    private User user;
    private String year,month,day;
    private FragmentManager fragmentManager;
    private RxManager rxManager = new RxManager();
    private boolean isMap = false;
    private WorkingFragment workingFragment;
    private HotMapFragment hotMapFragment;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.toolbar_bg),0);
        user = SpUtil.getUser();
        fragmentManager = getChildFragmentManager();
        setView();
    }

    private void setView(){
        //getDate();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        workingFragment = new WorkingFragment();
        transaction.replace(R.id.fl_home,workingFragment).commit();
    }
    private void getDate(){
        if (year == null || month == null || day == null){
            Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR)+"";
            month = c.get(Calendar.MONTH)+"";
            day = c.get(Calendar.DAY_OF_MONTH)+"";
        }
    }

    @OnClick({R.id.all_location,R.id.img_home_hotmap})
    public void onViewClicked(View view) {
        switch (view.getId()){
            /*case R.id.btn_home_order:
                //接单
//                Intent intent1 = new Intent(getContext(),MyService.class);
//                context.stopService(intent1);
                rxManager.post(Constant.CLOSE_TRACE,0);
                presenter.start(SpUtil.getUser().getCarrierId());
                break;*/
            case R.id.all_location:
                //JumpUtil.toMap(context);
                Intent intent = new Intent(getContext(), MyService.class);
                intent.putExtra("imei",SpUtil.getUser().getCarrierId());
                context.startService(intent);
                break;
            case R.id.img_home_hotmap:
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (isMap){
                    isMap = false;
                    imgMap.setImageResource(R.mipmap.nav_btn_heatmap);
                    if (workingFragment == null){
                        workingFragment = new WorkingFragment();
                    }
                    transaction.replace(R.id.fl_home,workingFragment).commit();
                }else {
                    isMap = true;
                    imgMap.setImageResource(R.mipmap.nav_btn_map);
                    if (hotMapFragment == null){
                        hotMapFragment = new HotMapFragment();
                    }
                    transaction.replace(R.id.fl_home,hotMapFragment).commit();
                }
                break;
        }

    }
    @Override
    public void updateLocation(BDLocation bdLocation){
        if (!TextUtils.isEmpty(bdLocation.getCity())){
            tvLocation.setText(bdLocation.getCity());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.unregister();
    }

}
